package pers.lt.admin.services;

import org.springframework.transaction.annotation.Transactional;
import pers.lt.hibernatebeans.*;
import pers.lt.tools.StringFacs;
import pers.lt.admin.dao.AdminDao;
import pers.lt.jdbc.Page;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 18621 on 2016/7/5.
 */
@Component("AdminServices")
@Transactional(readOnly=false)
public class AdminServices {
    @Resource
    private AdminDao adminDao;
    //登录
    public UserInfoEntity loginCheck(UserInfoEntity userInfoEntity){
        UserInfoEntity userInfoEntity1 =  adminDao.queryUser(userInfoEntity);
        //把密码去掉
        if(!StringFacs.isEmpty(userInfoEntity1))userInfoEntity1.setPassword("");
        return userInfoEntity1;
    }
    //查询用户
    public UserInfoEntity userSelect(UserInfoEntity userInfoEntity){
        //如果ID为空，说明是新增，给他New一个ID
        UserInfoEntity a = adminDao.userSelect(userInfoEntity);
        return a;
    }
    //新增用户
    public UserInfoEntity userSave(UserInfoEntity userInfoEntity){
        //如果ID为空，说明是新增，给他New一个ID
        if(StringFacs.isEmpty(userInfoEntity.getId())) userInfoEntity.setId(StringFacs.getGuid32());
        UserInfoEntity a = adminDao.userSave(userInfoEntity);
        return a;
    }
    //删除用户
    public void userDelete(UserInfoEntity userInfoEntity){
        String [] ary= userInfoEntity.getId().split("~");
        for(String str:ary){
            UserInfoEntity userInfoEntity1 = new UserInfoEntity();
            userInfoEntity1.setId(str);
            if(!StringFacs.isEmpty(str))adminDao.userDelete(adminDao.userSelect(userInfoEntity1));
        }
    }
    //删除角色
    public void roleDelete(RoleInfoEntity roleInfoEntity){
        String [] ary= roleInfoEntity.getId().split("~");
        for(String str:ary){
            RoleInfoEntity roleInfoEntity1 = new RoleInfoEntity();
            roleInfoEntity1.setId(str);
            if(!StringFacs.isEmpty(str))adminDao.roleDelete(adminDao.roleSelect(roleInfoEntity1));
        }
    }
    //删除权限
    public void rightsDelete(RightsInfoEntity rightsInfoEntity){
        String [] ary= rightsInfoEntity.getId().split("~");
        for(String str:ary){
            RightsInfoEntity rightsInfoEntity1 = new RightsInfoEntity();
            rightsInfoEntity1.setId(str);
            if(!StringFacs.isEmpty(str))adminDao.rightsDelete(adminDao.selectRights(rightsInfoEntity1));
        }
    }
    //查询用户列表
    public Page userList(UserInfoEntity userInfoEntity,Page page){
        return adminDao.userList(userInfoEntity,page);
    }
    //查询角色
    public RoleInfoEntity roleSelect(RoleInfoEntity roleInfoEntity){
        return adminDao.roleSelect(roleInfoEntity);
    }
    //新增角色
    public RoleInfoEntity addRole(RoleInfoEntity roleInfoEntity){
        //如果ID为空，说明是新增，给他New一个ID
        if(StringFacs.isEmpty(roleInfoEntity.getId())) roleInfoEntity.setId(StringFacs.getGuid32());
        RoleInfoEntity a = adminDao.addRole(roleInfoEntity);
        return a;
    }
    //查询角色列表
    public Page roleList(RoleInfoEntity roleInfoEntity,Page page){
        return adminDao.roleList(roleInfoEntity, page);
    }
    //新增权限
    public RightsInfoEntity addRights(RightsInfoEntity rightsInfoEntity){
        //如果ID为空，说明是新增，给他New一个ID
        if(StringFacs.isEmpty(rightsInfoEntity.getId())) rightsInfoEntity.setId(StringFacs.getGuid32());
        RightsInfoEntity a = adminDao.addRights(rightsInfoEntity);
        return a;
    }
    public RightsInfoEntity selectRights(RightsInfoEntity rightsInfoEntity){
        RightsInfoEntity a = adminDao.selectRights(rightsInfoEntity);
        return a;
    }
    //查询权限列表
    public List<Map> rightsList(RightsInfoEntity rightsInfoEntity){
        List <Map> rList = new ArrayList<Map>();
        List <Map> list = adminDao.rightsList(rightsInfoEntity);

        for(int i=0 ;i<list.size();i++){
            Map map = list.get(i);
            int childCount  = 0;
            if(!StringFacs.isEmpty(map.get("child"))) childCount = Integer.parseInt(map.get("child").toString());
            if(childCount>0){
                Map mapT = new HashMap();
                mapT.put("id",map.get("id")+"_1");
                mapT.put("right_name","Ttest");
                List a = new ArrayList<Map>();
                a.add(mapT);
                map.put("children",a);
                map.put("state","closed");
            }else map.put("children",new ArrayList<Map>());
            rList.add(map);
        }
        return rList;
    }
    //保存用户角色对应关系
    public List userRoleSave(String parm,String paUserid) throws SQLException {
        //如果ID为空，说明是新增，给他New一个ID
        //if(StringFacs.isEmpty(roleUserInfoEntity.getId())) roleUserInfoEntity.setId(StringFacs.getGuid32());
        //RoleUserInfoEntity a = adminDao.userRoleSave(roleUserInfoEntity);
        JSONArray jsonArray = JSONArray.fromObject(parm);
        //先执行删除
        adminDao.userRoleDelete(paUserid);
        //执行新增
        for(int i = 0;i<jsonArray.size();i++) {
            Map map = jsonArray.getJSONObject(i);
            RoleUserInfoEntity a = new RoleUserInfoEntity();
            a.setId(StringFacs.getGuid32());
            a.setRoleId(map.get("id").toString());
            a.setUserId(paUserid);
            adminDao.userRoleSave(a);
        }
        List list = new ArrayList();
        Map map = new HashMap();
        map.put("a","123");
        list.add(map);
        //new ArrayList();
        return list;
    }
    //查询用户角色信息
    public Page userRoleList(RoleUserInfoEntity roleUserInfoEntity,Page page){
        return adminDao.userRoleList(roleUserInfoEntity, page);
    }
    //保存角色权限对应关系
    public RoleRightsInfoEntity roleRightsSave(RoleRightsInfoEntity roleRightsInfoEntity){
        //如果ID为空，说明是新增，给他New一个ID
        if(StringFacs.isEmpty(roleRightsInfoEntity.getId())) roleRightsInfoEntity.setId(StringFacs.getGuid32());
        RoleRightsInfoEntity a = adminDao.roleRightsSave(roleRightsInfoEntity);
        return a;
    }
    //保存角色权限对应关系
    public List roleRightsSave(String parm,String paRoleId) throws SQLException {
        JSONArray jsonArray = JSONArray.fromObject(parm);
        //先执行删除
        adminDao.roleRightsDelete(paRoleId);
        //执行新增
        for(int i = 0;i<jsonArray.size();i++) {
            Map map = jsonArray.getJSONObject(i);
            RoleRightsInfoEntity a = new RoleRightsInfoEntity();
            a.setId(StringFacs.getGuid32());
            a.setRightId(map.get("id").toString());
            a.setRoleId(paRoleId);
            adminDao.roleRightsSave(a);
        }
        List list = new ArrayList();
        return list;
    }
    //查询角色权限信息
    public List roleRightsList(RoleRightsInfoDto roleRightsInfoDto){
        //查询角色对应的权限信息，组织成树形结构
        //{ id:1, pId:0, name:"随意勾选 1", open:true},
        List<Map> list = adminDao.roleRightsList(roleRightsInfoDto);
        List rlist = new ArrayList();
        for(int i=0;i<list.size();i++){
            Map map  = list.get(i);
            Map rmap = new HashMap();
            //id
            rmap.put("id",map.get("id"));
            //父节点
            rmap.put("pId",StringFacs.isEmpty(map.get("pid"))?0:map.get("pid"));
            rmap.put("name",map.get("right_name"));
            //是否有子节点
            String childs = childs= StringFacs.isEmpty(map.get("children"))?"0":map.get("children").toString();
            int childCount = Integer.parseInt(childs);
            rmap.put("isParent",childCount>0?true:false);
            //是否选中,通过判断 角色ID为不为空来判断 是否这个角色有这个权限，
            rmap.put("checked",StringFacs.isEmpty(map.get("role_id"))?false:true);
            rlist.add(rmap);
        }
        return rlist;
    }
}
